﻿using MetroFramework.Forms;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using YunNanHealthProject_Register.Entity;
using YunNanHealthProject_Register.Model;
using YunNanHealthProject_Register.Utils;

namespace YunNanHealthProject_Register.View
{
    public partial class HospitalInfoForm : MetroForm
    {
        private BasicInfo basicInfo = new BasicInfo();
        string tempPicture = "";
        string tempLogo = "";

        public HospitalInfoForm()
        {
            InitializeComponent();
        }


        private void OKButton_Click(object sender, EventArgs e)
        {

            HospitalInfo hospital = basicInfo.GetHosPitalInfo();
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("Request");
            xmldoc.AppendChild(xmlRoot);
            //信息头
            XmlElement messageHeader = xmldoc.CreateElement("MessageHeader");
            //账号信息
            XmlElement accessAccount = xmldoc.CreateElement("accessAccount");
            XmlText accessAccountText = xmldoc.CreateTextNode("43120477700");
            accessAccount.AppendChild(accessAccountText);
            //签名
            XmlElement sign = xmldoc.CreateElement("sign");
            XmlText signText = xmldoc.CreateTextNode("43120477700");
            sign.AppendChild(signText);
            //完成头部信息
            messageHeader.AppendChild(accessAccount);
            messageHeader.AppendChild(sign);
            xmlRoot.AppendChild(messageHeader);
            //医院信息
            XmlElement hosInfo = xmldoc.CreateElement("HosInfo");
            //医院代码
            XmlElement hosOrgCode = xmldoc.CreateElement("hosOrgCode");
            XmlText hosOrgCodeText = xmldoc.CreateTextNode(hospital.hosOrgCode);
            hosOrgCode.AppendChild(hosOrgCodeText);
            hosInfo.AppendChild(hosOrgCode);
            //卫生机构代码
            XmlElement orgCode = xmldoc.CreateElement("orgCode");
            XmlText orgCodeText = xmldoc.CreateTextNode(hospital.orgCode);
            orgCode.AppendChild(orgCodeText);
            hosInfo.AppendChild(orgCode);
            //医院名称
            XmlElement hosName = xmldoc.CreateElement("hosName");
            XmlText hosNameText = xmldoc.CreateTextNode(hospital.hosName);
            hosName.AppendChild(hosNameText);
            hosInfo.AppendChild(hosName);
            //医院地址
            XmlElement hospitalAdd = xmldoc.CreateElement("hospitalAdd");
            XmlText hospitalAddText = xmldoc.CreateTextNode(AddressTextBox.Text);
            hospitalAdd.AppendChild(hospitalAddText);
            hosInfo.AppendChild(hospitalAdd);
            //预约挂号须知
            XmlElement hospitalRule = xmldoc.CreateElement("hospitalRule");
            XmlText hospitalRuleText = xmldoc.CreateTextNode(NoteRichTextBox.Text.Replace("\r\n", ""));
            hospitalRule.AppendChild(hospitalRuleText);
            hosInfo.AppendChild(hospitalRule);
            //医院网址
            XmlElement hospitalWeb = xmldoc.CreateElement("hospitalWeb");
            XmlText hospitalWebText = xmldoc.CreateTextNode(HttpAddressTextBox.Text);
            hospitalWeb.AppendChild(hospitalWebText);
            hosInfo.AppendChild(hospitalWeb);
            //交通指南
            XmlElement trafficGuide = xmldoc.CreateElement("trafficGuide");
            XmlText trafficGuideText = xmldoc.CreateTextNode(TrafficRichTextBox.Text);
            trafficGuide.AppendChild(trafficGuideText);
            hosInfo.AppendChild(trafficGuide);
            //医院简介
            XmlElement hospitalDesc = xmldoc.CreateElement("hospitalDesc");
            XmlText hospitalDescText = xmldoc.CreateTextNode(IntroducrichTextBox.Text);
            hospitalDesc.AppendChild(hospitalDescText);
            hosInfo.AppendChild(hospitalDesc);
            //联系电话
            XmlElement hospitalTel = xmldoc.CreateElement("hospitalTel");
            XmlText hospitalTelText = xmldoc.CreateTextNode(hospital.hospitalTel);
            hospitalTel.AppendChild(hospitalTelText);
            hosInfo.AppendChild(hospitalTel);
            //医院级别
            XmlElement hospitalGrade = xmldoc.CreateElement("hospitalGrade");
            XmlText hospitalGradeText = xmldoc.CreateTextNode(hospital.hospitalGrade);
            hospitalGrade.AppendChild(hospitalGradeText);
            hosInfo.AppendChild(hospitalGrade);
            //医院等级
            XmlElement hospitalLevel = xmldoc.CreateElement("hospitalLevel");
            XmlText hospitalLevelText = xmldoc.CreateTextNode(hospital.hospitalLevel);
            hospitalLevel.AppendChild(hospitalLevelText);
            hosInfo.AppendChild(hospitalLevel);
            //医院类别
            XmlElement hospitalType = xmldoc.CreateElement("hospitalType");
            XmlText hospitalTypeText = xmldoc.CreateTextNode(hospital.hospitalType);
            hospitalType.AppendChild(hospitalTypeText);
            hosInfo.AppendChild(hospitalType);
            //支付方式
            XmlElement payMode = xmldoc.CreateElement("payMode");
            XmlText payModeText;
            string temp = "";
            if (WeiXinCheckBox.Checked)
            {
                temp = "1;";
            }
            if (ZhiFuBaoCheckBox.Checked)
            {
                temp = temp + "2;";
            }
            if (HospitalCheckBox.Checked)
            {
                temp = temp + "3;";
            }
            if (YiBaoCheckBox.Checked)
            {
                temp = temp + "5;";
            }
            if (BankCheckBox.Checked)
            {
                temp = temp + "6;";
            }
            if (PreferentialCheckBox.Checked)
            {
                temp = temp + "7;";
            }
            if (AccountCheckBox.Checked)
            {
                temp = temp + "8;";

            }
            if (OtheCheckBox.Checked)
            {
                temp = temp + "99;";
            }
            

            payModeText = xmldoc.CreateTextNode(temp.Substring(0,temp.Length -1));
            payMode.AppendChild(payModeText);
            hosInfo.AppendChild(payMode);
            //预约方式
            XmlElement orderMode = xmldoc.CreateElement("orderMode");
            XmlText orderModeText;
            if (AppointComboBox.Text.Equals("有卡预约"))
            {
                orderModeText = xmldoc.CreateTextNode("1");
            }
            else
            {
                orderModeText = xmldoc.CreateTextNode("2");
            }
            orderMode.AppendChild(orderModeText);
            hosInfo.AppendChild(orderMode);
            //是否展开预约
            XmlElement isRegister = xmldoc.CreateElement("isRegister");
            XmlText isRegisterText;
            if (IsAppointComboBox.Text.Equals("是"))
            {
                isRegisterText = xmldoc.CreateTextNode("0");
            }
            else
            {
                isRegisterText = xmldoc.CreateTextNode("1");
            }
            isRegister.AppendChild(isRegisterText);
            hosInfo.AppendChild(isRegister);
            //是否支持分时段
            XmlElement isSpTime = xmldoc.CreateElement("isSpTime");
            XmlText isSpTimeText;
            if (IsStageComboBox.Text.Equals("是"))
            {
                isSpTimeText = xmldoc.CreateTextNode("0");
            }
            else
            {
                isSpTimeText = xmldoc.CreateTextNode("1");
            }
            //医院属性
            XmlElement orgSort = xmldoc.CreateElement("orgSort");
            XmlText orgSortText = xmldoc.CreateTextNode("3");
            orgSort.AppendChild(orgSortText);
            hosInfo.AppendChild(orgSort);
            //横坐标
            XmlElement mapInfoX = xmldoc.CreateElement("mapInfoX");
            //102.719664(高德地图版)
            XmlText mapInfoXText = xmldoc.CreateTextNode("102.719664");
            mapInfoX.AppendChild(mapInfoXText);
            hosInfo.AppendChild(mapInfoX);
            //纵坐标
            XmlElement mapInfoY = xmldoc.CreateElement("mapInfoY");
            //25.028048(高德地图版)
            XmlText mapInfoYText = xmldoc.CreateTextNode("25.028048");
            mapInfoY.AppendChild(mapInfoYText);
            hosInfo.AppendChild(mapInfoY);
            //次序
            XmlElement indexNo = xmldoc.CreateElement("indexNo");
            XmlText indexNoText = xmldoc.CreateTextNode("1");
            indexNo.AppendChild(indexNoText);
            hosInfo.AppendChild(indexNo);
            //号源模式
            XmlElement sourceModel = xmldoc.CreateElement("sourceModel");
            XmlText sourceModelText;
            if (TypeComboBox.Text.Equals("有无号源模式"))
            {
                sourceModelText = xmldoc.CreateTextNode("1");
            }
            else
            {
                sourceModelText = xmldoc.CreateTextNode("2");
            }
            sourceModel.AppendChild(sourceModelText);
            hosInfo.AppendChild(sourceModel);
            //层级类型
            XmlElement levelType = xmldoc.CreateElement("levelType");
            XmlText levelTypeText;
            if (LevelComboBox.Text.Equals("非层级"))
            {
                levelTypeText = xmldoc.CreateTextNode("2");
            }
            else
            {
                levelTypeText = xmldoc.CreateTextNode("1");
            }
            levelType.AppendChild(levelTypeText);
            hosInfo.AppendChild(levelType);
            //医院图片
            XmlElement hospitalImage = xmldoc.CreateElement("hospitalImage");
            XmlText hospitalImageText = xmldoc.CreateTextNode(tempPicture);
            hospitalImage.AppendChild(hospitalImageText);
            hosInfo.AppendChild(hospitalImage);
            //支付等待时间
            XmlElement payWaitTime = xmldoc.CreateElement("payWaitTime");
            XmlText payWaitTimeText = xmldoc.CreateTextNode(TimeTextBox.Text);
            payWaitTime.AppendChild(payWaitTimeText);
            hosInfo.AppendChild(payWaitTime);
            //医院简称
            XmlElement shortName = xmldoc.CreateElement("shortName");
            XmlText shortNameText = xmldoc.CreateTextNode(ShortTextBox.Text);
            shortName.AppendChild(shortNameText);
            hosInfo.AppendChild(shortName);
            //所属区县代码
            XmlElement hospitalArea = xmldoc.CreateElement("hospitalArea");
            XmlText HospitalAreaText = xmldoc.CreateTextNode("530100");
            hospitalArea.AppendChild(HospitalAreaText);
            hosInfo.AppendChild(hospitalArea);
            //是否医保
            XmlElement isMedicalInsurance = xmldoc.CreateElement("isMedicalInsurance");
            XmlText isMedicalInsuranceText;
            if (IsYiBaoComboBox.Text.Equals("否"))
            {
                isMedicalInsuranceText = xmldoc.CreateTextNode("1");
            }
            else
            {
                isMedicalInsuranceText = xmldoc.CreateTextNode("0");
            }
            isMedicalInsurance.AppendChild(isMedicalInsuranceText);
            hosInfo.AppendChild(isMedicalInsurance);
            //市级代码
            XmlElement cityArea = xmldoc.CreateElement("cityArea");
            XmlText cityAreaText = xmldoc.CreateTextNode("530100");
            cityArea.AppendChild(cityAreaText);
            hosInfo.AppendChild(cityArea);
            //取号需知
            XmlElement numGuide = xmldoc.CreateElement("numGuide");
            XmlText numGuideText = xmldoc.CreateTextNode(NumGuideTextBox.Text);
            numGuide.AppendChild(numGuideText);
            hosInfo.AppendChild(numGuide);
            //医院logo图片信息
            XmlElement hospitalLogo = xmldoc.CreateElement("hospitalLogo");
            XmlText hospitalLogoText = xmldoc.CreateTextNode(tempLogo);
            hospitalLogo.AppendChild(hospitalLogoText);
            hosInfo.AppendChild(hospitalLogo);
            xmlRoot.AppendChild(hosInfo);

            MessageBox.Show(xmldoc.InnerXml);
            SaveTempData(".\\config\\TextFile.ini");

            WebReference.AppointmentServer appointmentServer = new WebReference.AppointmentServer();
            string upHospitalInfo = appointmentServer.UpHospitalInfoService(xmldoc.InnerXml);
            MessageBox.Show(GetResult(upHospitalInfo));
        }

        private void HospitalInfoForm_Load(object sender, EventArgs e)
        {
            LoadTempData(".\\config\\TextFile.ini");
        }

        //界面赋值
        private void LoadTempData(string path)
        {
            try
            {

                //医院地址
                AddressTextBox.Text = IniHelper.ReadIni("HospitalInfo", "hospitalAddress", path);
                HttpAddressTextBox.Text = IniHelper.ReadIni("HospitalInfo", "hospitalNet", path);
                WeiXinCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "ZhiFuBao", path).ToLower());
                ZhiFuBaoCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "ZhiFuBao", path).ToLower());
                HospitalCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "Hospital", path).ToLower());
                YiBaoCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "YiBao", path).ToLower());
                BankCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "Bank", path).ToLower());
                PreferentialCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "Preferential", path).ToLower());
                AccountCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "Account", path).ToLower());
                OtheCheckBox.Checked = bool.Parse(IniHelper.ReadIni("payType", "Other", path).ToLower());
                AppointComboBox.Text = IniHelper.ReadIni("Describe", "AppointType", path);
                IsAppointComboBox.Text = IniHelper.ReadIni("Describe", "IsAppoint", path);
                LevelComboBox.Text = IniHelper.ReadIni("Describe", "LevelType", path);
                IsYiBaoComboBox.Text = IniHelper.ReadIni("Describe", "IsYiBao", path);
                TypeComboBox.Text = IniHelper.ReadIni("Describe", "Type", path);
                IsStageComboBox.Text = IniHelper.ReadIni("Describe", "IsStage", path);
                TimeTextBox.Text = IniHelper.ReadIni("Describe", "Time", path);
                ShortTextBox.Text = IniHelper.ReadIni("Describe", "ShorName", path);
                NoteRichTextBox.Text = IniHelper.ReadIni("Note", "Registered", path);
                TrafficRichTextBox.Text = IniHelper.ReadIni("Note", "Traffic", path);
                IntroducrichTextBox.Text = IniHelper.ReadIni("Note", "Introducrich", path);
                SencePictureBox.Image = Image.FromFile(IniHelper.ReadIni("Picture", "scence", path));
                LogoPictureBox.Image = Image.FromFile(IniHelper.ReadIni("Picture", "logo", path));
                NumGuideTextBox.Text = IniHelper.ReadIni("NumGuide", "Guide", path);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }

        }

        //界面存值
        private void SaveTempData(string path)
        {
            IniHelper.WriteIni("HospitalInfo", "hospitalAddress", AddressTextBox.Text, path);
            IniHelper.WriteIni("HospitalInfo", "hospitalNet", HttpAddressTextBox.Text, path);
            IniHelper.WriteIni("payType", "WeiXin", WeiXinCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "ZhiFuBao", ZhiFuBaoCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "Hospital", HospitalCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "YiBao", YiBaoCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "Bank", BankCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "Preferential", PreferentialCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "Account", AccountCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("payType", "Other", OtheCheckBox.Checked.ToString(), path);
            IniHelper.WriteIni("Describe", "AppointType", AppointComboBox.Text, path);
            IniHelper.WriteIni("Describe", "IsAppoint", IsAppointComboBox.Text, path);
            IniHelper.WriteIni("Describe", "LevelType", LevelComboBox.Text, path);
            IniHelper.WriteIni("Describe", "IsYiBao", IsYiBaoComboBox.Text, path);
            IniHelper.WriteIni("Describe", "Type", TypeComboBox.Text, path);
            IniHelper.WriteIni("Describe", "IsStage", IsStageComboBox.Text, path);
            IniHelper.WriteIni("Describe", "Time", TimeTextBox.Text, path);
            IniHelper.WriteIni("Describe", "ShorName", ShortTextBox.Text, path);
            IniHelper.WriteIni("Note", "Registered", NoteRichTextBox.Text.Replace("\r\n", " "), path);
            IniHelper.WriteIni("Note", "Traffic", TrafficRichTextBox.Text, path);
            IniHelper.WriteIni("Note", "Introducrich", IntroducrichTextBox.Text, path);
            IniHelper.WriteIni("Picture", "scence", ".\\image\\HospitalSence.png", path);
            IniHelper.WriteIni("Picture", "logo", ".\\image\\HospitalLogo.png", path);
            IniHelper.WriteIni("NumGuide", "Guide", NumGuideTextBox.Text, path);
        }

        private void TimeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }

        }

        //医院logo
        private void LogoButton_Click(object sender, EventArgs e)
        {
            tempLogo = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Title = "选择要转换的图片";
            dlg.Filter = "Image files (AllFiles (*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    tempLogo = tempLogo + ImgToBase64String(dlg.FileNames[i].ToString(), LogoPictureBox);
                }
                if (false == Directory.Exists(Application.StartupPath + "\\Image\\"))
                {
                    Directory.CreateDirectory(Application.StartupPath + "\\Image\\");
                }
                File.Copy(dlg.FileName, Application.StartupPath + "\\Image\\HospitalLogo.png", true);
            }
        }

        //图片上传医院全景图
        private void PictureButton_Click(object sender, EventArgs e)
        {
            tempPicture = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Title = "选择要转换的图片";
            dlg.Filter = "AllFiles (*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    tempPicture = tempPicture + ImgToBase64String(dlg.FileNames[i].ToString(), SencePictureBox);
                }
                if (false == Directory.Exists(Application.StartupPath + "\\Image\\"))
                {
                    Directory.CreateDirectory(Application.StartupPath + "\\Image\\");
                }
                File.Copy(dlg.FileName, Application.StartupPath + "\\Image\\HospitalSence.png", true);
            }
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //图片 转为  base64编码的文本
        private string ImgToBase64String(string Imagefilename, PictureBox pictureBox)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                pictureBox.Image = bmp;

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
                //MessageBox.Show("转换成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
                return null;
            }
        }

        //解析返回值
        public string GetResult(string xmlStr)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xmlStr);

            var rowNoteList = doc.SelectNodes("/Response/MessageHeader");


            if (rowNoteList != null)
            {
                foreach (XmlNode rowNode in rowNoteList)
                {
                    var fieldNodeList = rowNode.ChildNodes;
                    foreach (XmlNode courseNode in fieldNodeList)
                    {
                        if (courseNode.Name.Equals("desc"))
                        {
                            return courseNode.InnerText;
                        }
                    }
                }
            }
            return "无返回值";
        }
    }
}
